//
//  GRbarginingCell.m
//  YC_GRice
//
//  Created by luojinrong11 on 2019/4/16.
//  Copyright © 2019年 YeCai. All rights reserved.
//

#import "GRbarginingCell.h"

@implementation GRbarginingCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (IBAction)barginAction:(UIButton *)sender {
    if (_delegate &&[_delegate respondsToSelector:@selector(skipToBarginPage:)]) {
       
            [_delegate skipToBarginPage:self.haggleModel.id ];

    }
    
}

-(void)setHaggleModel:(GRhaggleModel *)haggleModel{
    _haggleModel = haggleModel;
    self.nameLabel.text = haggleModel.goodModel.title;
    [self.goodImageView sd_setImageWithURL:[NSURL URLWithString:haggleModel.goodModel.mainPhoto] placeholderImage:[UIImage imageNamed:@""]];
    if ([haggleModel.status isEqualToString:@"processing"]) {
        self.successLabel.hidden = YES;
        self.timeLabel.hidden = NO;
        self.timeLabel.text = [NSString stringWithFormat:@"%@后结束",[self getTimeStr:haggleModel.expiresIn ]];
        NSString * priceTex = [NSString stringWithFormat:@"已砍%.2lf元",haggleModel.currPrice] ;
  
        NSMutableAttributedString * attribute = [[NSMutableAttributedString alloc]initWithString:priceTex attributes: @{NSFontAttributeName:[UIFont systemFontOfSize:12],NSForegroundColorAttributeName:HEXCOLOR(0x666666)}];
        [attribute setAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12],NSForegroundColorAttributeName:HEXCOLOR(0xFE6E0F)} range:NSMakeRange(2, priceTex.length-3)];
        
        self.bargainPriceLabel.attributedText = attribute;
        self.bargainPriceLabel.hidden = NO;
          [self.statusBt setTitle:@"继续砍价" forState:  UIControlStateNormal]    ;
    }else{
      self.successLabel.hidden = NO;
      self.timeLabel.hidden = YES;
      self.bargainPriceLabel.hidden = YES;
      
        [self.statusBt setTitle:@"前往查看" forState:  UIControlStateNormal]    ;
    }
  
}


-(NSString *)getTimeStr:(NSInteger)timeStamp{
    NSString *time;
    if (timeStamp < 0) {
        time = @"0";
    }else{
        int day = (int)(timeStamp / (3600 * 24));
        int hour = (int)((timeStamp - day * 3600 * 24) / 3600);
        int minute = (int)(timeStamp - day * 3600 * 24 - hour * 3600) / 60;
        int second = (int)(timeStamp - day * 3600 * 24 - hour * 3600 - minute * 60);
        
        if (day <= 0&& hour > 0) {
            if (hour <= 0 && minute > 0) {
                time = [NSString stringWithFormat:@"%02d:%02d",minute, second];
                
            }else if (minute <= 0 && hour <= 0){
                
                if (second >= 10) {
                    time = [NSString stringWithFormat:@"%02d",second];
                }else {
                    time = [NSString stringWithFormat:@"%01d",second];
                }
                
            }else{
                
                time = [NSString stringWithFormat:@"%02d:%02d:%02d",hour, minute, second];
            }
        }else{
            time = [NSString stringWithFormat:@"%02d:%02d:%02d:%02d",day,hour,minute,second];
        }
    }
    return time;
}

@end
